package com.lvideo.http.impl;

import android.content.Context;
import android.util.Log;

import com.lvideo.http.LVideoHttpJavaHandler;
import com.lvideo.http.LVideoHttpLog;
import com.lvideo.http.bean.LVideoBaseBean;
import com.lvideo.http.bean.LVideoDataHull;
import com.lvideo.http.exception.DataIsErrException;
import com.lvideo.http.exception.DataIsNullException;
import com.lvideo.http.exception.DataNoUpdateException;
import com.lvideo.http.exception.JsonCanNotParseException;
import com.lvideo.http.exception.ParseException;
import com.lvideo.http.parse.LVideoBaseParser;

import java.io.IOException;

/**
 * 请求封装内
 */
public class LVideoHttpTool<T extends LVideoBaseBean> {

    public <D> LVideoDataHull<T> requsetData(LVideoHttpBaseParameter<T, D, ?> httpParameter, Context context) {
        LVideoDataHull<T> dataHull;
        if (httpParameter == null) {
            dataHull = new LVideoDataHull<T>();
            dataHull.setDataType(LVideoDataHull.DataType.PARAMS_IS_NULL);
            LVideoHttpLog.Err("Parameter is null");
            return dataHull;
        }
        if(httpParameter.getBaseUrl() != null && httpParameter.getBaseUrl().contains("https")){
            if(httpParameter.getType() == LVideoHttpParameter.Type.GET){
                httpParameter.setType(LVideoHttpParameter.Type.HTTPSGET);
            } else if(httpParameter.getType() == LVideoHttpParameter.Type.POST){
                httpParameter.setType(LVideoHttpParameter.Type.HTTPSPOST);
            } else {

            }
        }

        if (httpParameter.getType() == LVideoHttpParameter.Type.GET) {
            dataHull = doGet(httpParameter);
        } else if (httpParameter.getType() == LVideoHttpParameter.Type.POST) {
            dataHull = doPost(httpParameter);
        }  else if(httpParameter.getType() == LVideoHttpParameter.Type.HTTPSGET){
            dataHull = doHttpsGet(httpParameter, context);
        }else if(httpParameter.getType() == LVideoHttpParameter.Type.HTTPSPOST){
            dataHull = doHttpsPost(httpParameter, context);
        } else {
            dataHull = new LVideoDataHull<T>();
            dataHull.setDataType(LVideoDataHull.DataType.REQUESTMETHOD_IS_ERR);
            LVideoHttpLog.Err("RequestMethod is error");
            return dataHull;
        }
        dataHull.setUpdataId(httpParameter.getUpdataId());

        return dataHull;
    }

    private <D> LVideoDataHull<T> doGet(LVideoHttpBaseParameter<T, D, ?> httpParameter) {
        LVideoBaseParser<T, D> parser = httpParameter.getParser();
        String response = null;
        LVideoDataHull<T> dataHull = new LVideoDataHull<T>();

        try {
            LVideoHttpJavaHandler handler = new LVideoHttpJavaHandler();
            response = handler.doGet(httpParameter);
            if (parser != null) {
                dataHull.setSourceData(response);
                dataHull.setDataEntity(parser.initialParse(response));
                dataHull.setDataType(LVideoDataHull.DataType.DATA_IS_INTEGRITY);
                LVideoHttpLog.Err("complete!");
                dataHull.setCode(parser.getCode());
                dataHull.setMsg(parser.getMsg());
                return dataHull;
            } else {
                dataHull.setDataType(LVideoDataHull.DataType.DATA_PARSER_IS_NULL);
                LVideoHttpLog.Err("Do not have parser");
            }
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("PlayMediaController", "msg is " + e.getMessage());
            dataHull.setDataType(LVideoDataHull.DataType.CONNECTION_FAIL);
            dataHull.setmHttpCode(e.getMessage());
            LVideoHttpLog.Err("connected is fail");
        } catch (ParseException e) {
            e.printStackTrace();
            dataHull.setDataType(LVideoDataHull.DataType.DATA_PARSE_EXCEPTION);
            LVideoHttpLog.Err("parse error");
        } catch (DataIsNullException e) {
            e.printStackTrace();
            dataHull.setDataType(LVideoDataHull.DataType.DATA_IS_NULL);
            LVideoHttpLog.Err("data is null");
        } catch (JsonCanNotParseException e) {
            e.printStackTrace();
            dataHull.setDataType(LVideoDataHull.DataType.DATA_CAN_NOT_PARSE);
            dataHull.setErrMsg(parser.getErrorMsg());
            LVideoHttpLog.Err("canParse is false");
        } catch (DataIsErrException e) {
            e.printStackTrace();
            dataHull.setDataType(LVideoDataHull.DataType.DATA_IS_ERR);
            LVideoHttpLog.Err("data is err");
        } catch (DataNoUpdateException e) {
            e.printStackTrace();
            dataHull.setDataType(LVideoDataHull.DataType.DATA_NO_UPDATE);
            LVideoHttpLog.Err("data has not update");
        } finally {
            if (parser != null) {
                dataHull.setMessage(parser.getMessage());
                dataHull.setCode(parser.getCode());
            }
        }

        return dataHull;
    }

    private <D> LVideoDataHull<T> doPost(LVideoHttpBaseParameter<T, D, ?> httpParameter) {
        LVideoBaseParser<T, D> parser = httpParameter.getParser();
        String response = null;
        LVideoDataHull<T> dataHull = new LVideoDataHull<T>();

        try {
            LVideoHttpJavaHandler handler = new LVideoHttpJavaHandler();
            response = handler.doPost(httpParameter);

            if (parser != null) {
                dataHull.setDataType(LVideoDataHull.DataType.DATA_IS_INTEGRITY);
                dataHull.setDataEntity(parser.initialParse(response));
                dataHull.setSourceData(response);
                LVideoHttpLog.Err("complete!");
                dataHull.setCode(parser.getCode());
                dataHull.setMsg(parser.getMsg());
                return dataHull;
            } else {
                dataHull.setDataType(LVideoDataHull.DataType.DATA_PARSER_IS_NULL);
                LVideoHttpLog.Err("Do not have parser");
            }
        } catch (IOException e) {
            e.printStackTrace();
            dataHull.setDataType(LVideoDataHull.DataType.CONNECTION_FAIL);
            dataHull.setmHttpCode(e.getMessage());
            LVideoHttpLog.Err("connected is fail");
        } catch (ParseException e) {
            e.printStackTrace();
            dataHull.setDataType(LVideoDataHull.DataType.DATA_PARSE_EXCEPTION);
            LVideoHttpLog.Err("parse error");
        } catch (DataIsNullException e) {
            e.printStackTrace();
            dataHull.setDataType(LVideoDataHull.DataType.DATA_IS_NULL);
            LVideoHttpLog.Err("data is null");
        } catch (JsonCanNotParseException e) {
            e.printStackTrace();
            dataHull.setDataType(LVideoDataHull.DataType.DATA_CAN_NOT_PARSE);
            dataHull.setErrMsg(parser.getErrorMsg());
            LVideoHttpLog.Err("canParse is false");
        } catch (DataIsErrException e) {
            e.printStackTrace();
            dataHull.setDataType(LVideoDataHull.DataType.DATA_IS_ERR);
            LVideoHttpLog.Err("data is err");
        } catch (DataNoUpdateException e) {
            e.printStackTrace();
            dataHull.setDataType(LVideoDataHull.DataType.DATA_NO_UPDATE);
            LVideoHttpLog.Err("data has not update");
        } finally {
            if (parser != null) {
                dataHull.setMessage(parser.getMessage());
                dataHull.setCode(parser.getCode());
            }
        }

        return dataHull;
    }

    private <D> LVideoDataHull<T> doHttpsGet(LVideoHttpBaseParameter<T, D, ?> httpParameter, Context context) {
        LVideoBaseParser<T, D> parser = httpParameter.getParser();
        String response = null;
        LVideoDataHull<T> dataHull = new LVideoDataHull<T>();

        try {
            LVideoHttpJavaHandler handler = new LVideoHttpJavaHandler();
            response = handler.doHttpsGet(httpParameter, context);
            if (parser != null) {
                dataHull.setSourceData(response);
                dataHull.setDataEntity(parser.initialParse(response));
                dataHull.setDataType(LVideoDataHull.DataType.DATA_IS_INTEGRITY);
                LVideoHttpLog.Err("complete!");
                dataHull.setCode(parser.getCode());
                dataHull.setMsg(parser.getMsg());
                return dataHull;
            } else {
                dataHull.setDataType(LVideoDataHull.DataType.DATA_PARSER_IS_NULL);
                LVideoHttpLog.Err("Do not have parser");
            }
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("PlayMediaController", "msg is " + e.getMessage());
            dataHull.setDataType(LVideoDataHull.DataType.CONNECTION_FAIL);
            dataHull.setmHttpCode(e.getMessage());
            LVideoHttpLog.Err("connected is fail");
        } catch (ParseException e) {
            e.printStackTrace();
            dataHull.setDataType(LVideoDataHull.DataType.DATA_PARSE_EXCEPTION);
            LVideoHttpLog.Err("parse error");
        } catch (DataIsNullException e) {
            e.printStackTrace();
            dataHull.setDataType(LVideoDataHull.DataType.DATA_IS_NULL);
            LVideoHttpLog.Err("data is null");
        } catch (JsonCanNotParseException e) {
            e.printStackTrace();
            dataHull.setDataType(LVideoDataHull.DataType.DATA_CAN_NOT_PARSE);
            dataHull.setErrMsg(parser.getErrorMsg());
            LVideoHttpLog.Err("canParse is false");
        } catch (DataIsErrException e) {
            e.printStackTrace();
            dataHull.setDataType(LVideoDataHull.DataType.DATA_IS_ERR);
            LVideoHttpLog.Err("data is err");
        } catch (DataNoUpdateException e) {
            e.printStackTrace();
            dataHull.setDataType(LVideoDataHull.DataType.DATA_NO_UPDATE);
            LVideoHttpLog.Err("data has not update");
        } finally {
            if (parser != null) {
                dataHull.setMessage(parser.getMessage());
                dataHull.setCode(parser.getCode());
            }
        }

        return dataHull;
    }

    private <D> LVideoDataHull<T> doHttpsPost(LVideoHttpBaseParameter<T, D, ?> httpParameter, Context context) {
        LVideoBaseParser<T, D> parser = httpParameter.getParser();
        String response = null;
        LVideoDataHull<T> dataHull = new LVideoDataHull<T>();

        try {
            LVideoHttpJavaHandler handler = new LVideoHttpJavaHandler();
            response = handler.doHttpsPost(httpParameter, context);

            if (parser != null) {
                dataHull.setDataType(LVideoDataHull.DataType.DATA_IS_INTEGRITY);
                dataHull.setDataEntity(parser.initialParse(response));
                dataHull.setSourceData(response);
                LVideoHttpLog.Err("complete!");
                dataHull.setCode(parser.getCode());
                dataHull.setMsg(parser.getMsg());
                return dataHull;
            } else {
                dataHull.setDataType(LVideoDataHull.DataType.DATA_PARSER_IS_NULL);
                LVideoHttpLog.Err("Do not have parser");
            }
        } catch (IOException e) {
            e.printStackTrace();
            dataHull.setDataType(LVideoDataHull.DataType.CONNECTION_FAIL);
            dataHull.setmHttpCode(e.getMessage());
            LVideoHttpLog.Err("connected is fail");
        } catch (ParseException e) {
            e.printStackTrace();
            dataHull.setDataType(LVideoDataHull.DataType.DATA_PARSE_EXCEPTION);
            LVideoHttpLog.Err("parse error");
        } catch (DataIsNullException e) {
            e.printStackTrace();
            dataHull.setDataType(LVideoDataHull.DataType.DATA_IS_NULL);
            LVideoHttpLog.Err("data is null");
        } catch (JsonCanNotParseException e) {
            e.printStackTrace();
            dataHull.setDataType(LVideoDataHull.DataType.DATA_CAN_NOT_PARSE);
            dataHull.setErrMsg(parser.getErrorMsg());
            LVideoHttpLog.Err("canParse is false");
        } catch (DataIsErrException e) {
            e.printStackTrace();
            dataHull.setDataType(LVideoDataHull.DataType.DATA_IS_ERR);
            LVideoHttpLog.Err("data is err");
        } catch (DataNoUpdateException e) {
            e.printStackTrace();
            dataHull.setDataType(LVideoDataHull.DataType.DATA_NO_UPDATE);
            LVideoHttpLog.Err("data has not update");
        } finally {
            if (parser != null) {
                dataHull.setMessage(parser.getMessage());
                dataHull.setCode(parser.getCode());
            }
        }

        return dataHull;
    }
}
